package com.jbtm.mall.util;

import com.alibaba.fastjson.JSONObject;
import com.jbtm.mall.common.Constant;
import com.jbtm.mall.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Token服务
 *
 * @author
 * @createdate 04/18/2017
 */
@Service
public class TokenUtil {
    //Token的加密密钥
    private static String TOKEN_SECRET = "hsgZINiDdUoMviUhhIRbstMnCPWVnQzh";
    //Token的过期时长：7天
    private static Integer TOKEN_EXPIRE = 168;

    /**
     * 生成Token
     *
     * @param uid 用户ID
     * @return
     */
    public static String getToken(Integer uid) {
        JSONObject data = new JSONObject();
        data.put("uid", uid);
        data.put("t", System.currentTimeMillis());
        return SecurityUtil.base64Encrypt(SecurityUtil.des3Encrypt(TOKEN_SECRET, data.toJSONString()));
    }

    /**
     * 解析Token中获取uid
     *
     * @param token
     * @return
     */
    public static Integer resolveToken(String token) {
        Integer uid = 0;
        token = token.replace(" ", "+");
        String data = SecurityUtil.des3Decrypt(TOKEN_SECRET, SecurityUtil.base64Decrypt(token));
        if (StringUtils.isNotEmpty(data)) {
            JSONObject json = JSONObject.parseObject(data);
            uid = json.getInteger("uid");
        }
        return uid;
    }

    /**
     * 解析Token中获取时间
     *
     * @param token
     * @return
     */
    public static long resolveTime(String token) {
        long time = 0;
        token = token.replace(" ", "+");
        String data = SecurityUtil.des3Decrypt(TOKEN_SECRET, SecurityUtil.base64Decrypt(token));
        if (StringUtils.isNotEmpty(data)) {
            JSONObject json = JSONObject.parseObject(data);
            time = json.getLong("t");
        }
        return time;
    }

    /**
     * 验证请求参数是否合法
     *
     * @param requestParams
     * @return
     */
    public static Boolean authRequest(Map<String, String[]> requestParams) {
        try {
            if (null != requestParams && requestParams.size() > 0) {
                String token = requestParams.get("token")[0];
                token = token.replace(" ", "+"); //处理空格
                String timestamp = requestParams.get("timestamp")[0];
                String sid = requestParams.get("sid")[0];
                String signature = requestParams.get("signature")[0];
                //签名=MD5(sid+t+token)
                String sign = SecurityUtil.md5(sid + timestamp + token);
                if (null != sign && sign.equalsIgnoreCase(signature) && StringUtils.isNotEmpty(token)) {
                    String data = SecurityUtil.des3Decrypt(TOKEN_SECRET, SecurityUtil.base64Decrypt(token));
                    if (StringUtils.isNotEmpty(data)) {
                        JSONObject json = JSONObject.parseObject(data);
                        Long t = json.getLong("t");
                        Integer uid = json.getInteger("uid");
                        Long currentTime = System.currentTimeMillis();
                        Long expires = (currentTime - t) / 1000 / 3600; //转化成小时
                        //超时去登录页
                        if (expires < TOKEN_EXPIRE) {
                            if (expires / 60 > Constant.LOGIN_TIME_OUT) {
                                return false;
                            }
                            return true;
                        }
                    }
                }
            }
            return false;
        } catch (NullPointerException e) {
            new BizException("NullPointerException");
            return false;
        }
    }
}
